<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">搜索</label>
          <div class="layui-input-inline">
            <input type="text" name="keyword" placeholder="请输入关键词" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <label class="layui-form-label">所属分类</label>
          <div class="layui-input-inline">
            <script type="text/html" template lay-url="/api/news/getCategoryList"
              lay-done="layui.form.render('select');">
                <select name="category_id" id="category_id" required>
                     <option value="">请选择</option>
                      {{# layui.each(d.data,function(index,item){ }}
                     <option value="{{ item.id }}" {{ item.title === d.params.category_name ? 'selected' : '' }}>{{ item.title }}</option>
                      {{# }); }}
                </select>
              </script>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
          <button class="layui-btn layuiadmin-btn-list" data-type="add">添加文章</button>
          <button class="layui-btn layuiadmin-btn-list" data-type="batchdel">删除文章</button> 
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-list" data-type="recommending">推荐</button>
        <button class="layui-btn layuiadmin-btn-list" data-type="cancelRecommend">取消推荐</button>
        <button class="layui-btn layuiadmin-btn-list" data-type="topping">置顶</button>
        <button class="layui-btn layuiadmin-btn-list" data-type="cancelTop">取消置顶</button>
      </div>
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        <script type="text/html" id="switc_status">
          <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="switch" {{(d.status=='1')?'checked' : '' }}>
        </script>
      <script type="text/html" id="table-content-list">
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="show">预览</a>
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
  </div>
</div>

<script>
  layui.use('news', layui.factory('news')).use(['admin', 'news', 'table', 'form', 'layedit'], function () {
    var $ = layui.$
      , admin = layui.admin
      , view = layui.view
      , table = layui.table
      , layedit = layui.layedit
      , form = layui.form;

    form.render(null, 'app-content-list');

    //监听搜索
    form.on('submit(LAY-app-contlist-search)', function (data) {
      var field = data.field;

      console.log(field);
    //   console.log(11);

      //执行重载
      table.reload('LAY-app-content-list', {
        where: field
      });
    });

    var active = {
      batchdel: function () {
        var checkStatus = table.checkStatus('LAY-app-content-list')
          , checkData = checkStatus.data; //得到选中的数据

        if (checkData.length === 0) {
          return layer.msg('请选择数据');
        }

        var ids = [];   //声明数组
        for (var i = 0; i < checkData.length; i++) {
          ids.push(checkData[i].id);
        }


        layer.confirm('确定要删除所选数据?', function (index) {
          // console.log(ids);
          admin.req({
            type: "POST",
            url: '/api/news/del',
            data: { ids: ids },
            dataType: "json",
            success: function (res) {
              layer.msg(res.msg);
              table.reload('LAY-app-content-list');
            }
          });

        });

      },
      recommending: function () {
        var checkStatus = table.checkStatus('LAY-app-content-list')
          , checkData = checkStatus.data; //得到选中的数据

        if (checkData.length === 0) {
          return layer.msg('请选择数据');
        }

        var ids = [];   //声明数组
        for (var i = 0; i < checkData.length; i++) {
          ids.push(checkData[i].id);
        }
        admin.req({
            type: "POST",
            url: '/api/news/recommending',
            data: { ids: ids },
            dataType: "json",
            success: function (res) {
              layer.msg(res.msg);
              table.reload('LAY-app-content-list');
            }
          });
      },
      cancelRecommend: function () {
        var checkStatus = table.checkStatus('LAY-app-content-list')
          , checkData = checkStatus.data; //得到选中的数据

        if (checkData.length === 0) {
          return layer.msg('请选择数据');
        }

        var ids = [];   //声明数组
        for (var i = 0; i < checkData.length; i++) {
          ids.push(checkData[i].id);
        }
        admin.req({
            type: "POST",
            url: '/api/news/recommending',
            data: { ids: ids,status: 0 },
            dataType: "json",
            success: function (res) {
              layer.msg(res.msg);
              table.reload('LAY-app-content-list');
            }
          });
      },
      topping: function () {
        var checkStatus = table.checkStatus('LAY-app-content-list')
          , checkData = checkStatus.data; //得到选中的数据

        if (checkData.length === 0) {
          return layer.msg('请选择数据');
        }

        var ids = [];   //声明数组
        for (var i = 0; i < checkData.length; i++) {
          ids.push(checkData[i].id);
        }
        admin.req({
            type: "POST",
            url: '/api/news/topping',
            data: { ids: ids },
            dataType: "json",
            success: function (res) {
              layer.msg(res.msg);
              table.reload('LAY-app-content-list');
            }
          });
      },
      cancelTop: function () {
        var checkStatus = table.checkStatus('LAY-app-content-list')
          , checkData = checkStatus.data; //得到选中的数据

        if (checkData.length === 0) {
          return layer.msg('请选择数据');
        }

        var ids = [];   //声明数组
        for (var i = 0; i < checkData.length; i++) {
          ids.push(checkData[i].id);
        }
        admin.req({
            type: "POST",
            url: '/api/news/topping',
            data: { ids: ids,status: 0 },
            dataType: "json",
            success: function (res) {
              layer.msg(res.msg);
              table.reload('LAY-app-content-list');
            }
          });
      }

      //添加
      , add: function (othis) {
        admin.popup({
            title: '添加文章'
          , maxmin: true
          , area: ['1060px', '95%']
          , id: 'LAY-popup-content-add'
          , success: function (layero, index) {
            view(this.id).render('/news/add').done(function () {
              form.render(null, 'layuiadmin-app-form-list');

              //监听提交
              form.on('submit(layuiadmin-app-form-submit)', function (data) {
                var field = data.field; //获取提交的字段
                field.lang = lang

                if (field.status == 'on') {
                  field.status = 1;
                } else {
                  field.status = 0;
                }
                var content = ue.getContent();
                field.content = content;
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                admin.req({
                  type: "POST",
                  url: '/api/news/add',
                  data: { data: field },
                  dataType: "json",
                  success: function (res) {
                    if(res.code == 0){
                      layer.msg(res.msg, { time: 800 });
                      layui.table.reload('LAY-app-content-list'); //重载表格
                      layer.close(index); //执行关闭
                      // window.parent.location.reload() //刷新页面
                    }else{
                      layer.msg(res.msg, { time: 800 });
                    }
                  }
                });
              });
            });
          }
        });
      }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

  });
</script>

<script type="text/html" id="nameid">
{{# if(d.recommended_text == '推荐' && d.topped_text == '置顶'){ }}
<a class="layui-btn layui-btn-green layui-btn-xs">{{d.recommended_text}} </a><a class="layui-btn layui-btn-danger layui-btn-xs">{{d.topped_text}}</a> {{d.title}}
{{# }else if(d.recommended_text == '推荐'){ }}
<a class="layui-btn layui-btn-green layui-btn-xs">{{d.recommended_text}} </a> {{d.title}}
{{# }else if(d.topped_text == '置顶'){ }}
<a class="layui-btn layui-btn-danger layui-btn-xs">{{d.topped_text}} </a> {{d.title}}
{{# }else{ }}
{{d.title}}
{{# } }}
</script>